package com.sea.finance.controller;

import com.alibaba.excel.util.StringUtils;
import com.codecrane.core.util.DateUtils;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.sea.finance.entity.Vt;
import com.sea.finance.entity.VtFile;
import com.sea.finance.service.InventoryService;
import com.sea.finance.service.InventoryStockService;
import com.sea.finance.service.VtFileService;
import com.sea.finance.service.VtService;
import com.sea.operate.entity.RelatedSkuPrice;
import com.sea.operate.service.RelatedSkuPriceService;
import com.sea.supply.entity.BillRefused;
import com.sea.supply.service.BillRefusedService;
import com.sea.supply.service.ProducePaymentService;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * Created by 752322896 on 2019/5/4.
 */
@Component
public class TimerTask {

    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private BillRefusedService billRefusedService;
    @Autowired
    private ProducePaymentService producePaymentService;
    @Autowired
    private RelatedSkuPriceService relatedSkuPriceService;
    @Autowired
    private InventoryStockService inventoryStockService;


    @Autowired
    private VtService vtService;


    @Scheduled(cron = "0 0 1 * * ?")
    public void updateRelatedSalesPrice()  {
        List<RelatedSkuPrice> byDate = relatedSkuPriceService.findByDate(null);
        for(RelatedSkuPrice relatedSkuPrice:byDate) {
            //盘点当前时间如果超过结束时间 自动更改为原来的状态
            Integer day = 0;
            try {
                day = DateUtils.daysBetween(DateUtils.dateToDateTime(new Date()), relatedSkuPrice.getRelatedEndDate());
                if (day < 0) {
                    relatedSkuPrice.setFlag(99);
                    relatedSkuPriceService.updateByRelatedId(relatedSkuPrice);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    //供应商同步今天的报价
    @Scheduled(cron = "0 30 23 28-31 * ?")
    public void sendSupplierPrice()  {
        try {
            final Calendar c = Calendar.getInstance();
            if (c.get(Calendar.DATE) == c.getActualMaximum(Calendar.DATE)) {
                inventoryService.inventory(DateUtils.dateToString(new Date(), "yyyy-MM"));
                //inventoryService.updateChange(DateUtils.dateToString(new Date(), "yyyy-MM"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Scheduled(cron = "0 0 0 */1 * ?")
    public void updateBillRefused(){
        List<BillRefused> list = billRefusedService.findByCnd(null);
        for(BillRefused billRefused:list){
            if(null != billRefused.getRefusedBillMoney() && null != billRefused.getRefusedFinanceMoney()){
                if(billRefused.getRefusedBillMoney().equals(billRefused.getRefusedFinanceMoney())){
                    String refusedPlanNo = billRefused.getRefusedPlanNo();
                    producePaymentService.updateBillStatus(refusedPlanNo);
                }
            }
        }
    }

    @Scheduled(cron = "0 30 23 28-31 * ?")
    public void updateInventoryStock()  {
        try {
            inventoryStockService.updateByMonth();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 每天晚上12点同步V2数据
     */
    @Scheduled(cron = "0 23 18 * * ?")
    public void dealV2() {

//        VtFile vtFile = new VtFile();
//        vtFile.setFlag(1);
//        List<VtFile> list = vtFileService.findByCnd(vtFile);
//        if (null != list && list.size() != 0) {
//            for (VtFile v : list) {
//                //更新开始时间
//                vtFileService.usave(v);
//                //开始处理文件
//                String remark = vtFileService.dealFile(v.getId());
//                if (StringUtils.isNotBlank(remark)) {
//                    v.setFlag(2);
//                } else {
//                    v.setFlag(3);
//                    v.setRemark("更新成功！");
//                }
//                vtFileService.usave(v);
//            }
//        }
//        vtService.updateFeeName();
//        vtService.updateFnSku();
//        vtService.updateListingName();
    }

}
